/*
 * Copyright 2019 WeBank
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.webank.wedatasphere.dss.appconn.sparketl.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.lang.StringUtils;

/**
 * @author ***REMOVED***
 * @date 2021/7/12 10:58
 */
public class Sha256Utils {

    public static String getSHA256L32(String str) throws NoSuchAlgorithmException {
        try {
            // Generate a instance of sha-256
            MessageDigest hash = MessageDigest.getInstance("SHA-256");
            // calculate
            hash.update(str.getBytes());
            String result = new BigInteger(1, hash.digest()).toString(16);

            return StringUtils.leftPad(result, 32, '0');
        } catch (NoSuchAlgorithmException e) {
            throw new NoSuchAlgorithmException("Error generating sha256 of string", e);
        }
    }

}
